home *** CD-ROM | disk | FTP | other *** search
/ Inter.Net 55-1 / Inter.Net 55-1.iso / CBuilder / Info / TeachU14 / SAMS / Code / Day03 / referenc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-08  |  2.3 KB  |  96 lines

  1. //---------------------------------------------------------------------------
  2. #include <iostream.h>
  3. #include <conio.h>
  4. #include <stdlib.h>
  5. #pragma hdrstop
  6.  
  7. #include "structur.h"
  8.  
  9. void displayRecord(int, mailingListRecord mlRec);
  10.  
  11. int main(int, char**)
  12. {
  13.   cout << endl;
  14.   //
  15.   // create an array of mailingListRecord structures
  16.   //
  17.   mailingListRecord* listArray[3];
  18.   //
  19.   // create objects for each record
  20.   //
  21.   for (int i=0;i<3;i++)
  22.     listArray[i] = new mailingListRecord;
  23.   int index = 0;
  24.   //
  25.   // get three records
  26.   //
  27.   do {
  28.     // create a reference to the current record
  29.     mailingListRecord& rec = *listArray[index];
  30.     cout << "First Name: ";
  31.     cin.getline(rec.firstName, sizeof(rec.firstName) - 1);
  32.     cout << "Last Name: ";
  33.     cin.getline(rec.lastName, sizeof(rec.lastName) - 1);
  34.     cout << "Address: ";
  35.     cin.getline(rec.address, sizeof(rec.address) - 1);
  36.     cout << "City: ";
  37.     cin.getline(rec.city, sizeof(rec.city) - 1);
  38.     cout << "State: ";
  39.     cin.getline(rec.state, sizeof(rec.state) - 1);
  40.     char buff[10];
  41.     cout << "Zip: ";
  42.     cin.getline(buff, sizeof(buff) - 1);
  43.     rec.zip = atoi(buff);
  44.     index++;
  45.     cout << endl;
  46.   }
  47.   while (index < 3);
  48.   //
  49.   // display the three records
  50.   //
  51.   clrscr();
  52.   //
  53.   // must dereference the pointer to pass an object
  54.   // to the displayRecord function.
  55.   //
  56.   for (int i=0;i<3;i++) {
  57.     displayRecord(i, *listArray[i]);
  58.   }
  59.   //
  60.   // ask the user to choose a record
  61.   //
  62.   cout << "Choose a record: ";
  63.   int rec;
  64.   do {
  65.     rec = getch();
  66.     rec -= 49;
  67.   } while (rec < 0 || rec > 2);
  68.   //
  69.   // assign the selected record to a temporary variable
  70.   // must dereference here, too
  71.   //
  72.   mailingListRecord temp = *listArray[rec];
  73.   clrscr();
  74.   cout << endl;
  75.   //
  76.   // display the selected recrord
  77.   //
  78.   displayRecord(rec, temp);
  79.   getch();
  80.   return 0;
  81. }
  82. void displayRecord(int num, mailingListRecord mlRec)
  83. {
  84.   cout << "Record " << (num + 1) << ":" << endl;
  85.   cout << "Name:     " << mlRec.firstName << " ";
  86.   cout << mlRec.lastName;
  87.   cout << endl;
  88.   cout << "Address:  " << mlRec.address;
  89.   cout << endl << "          ";
  90.   cout << mlRec.city << ", ";
  91.   cout << mlRec.state << "  ";
  92.   cout << mlRec.zip;
  93.   cout << endl << endl;
  94. }
  95.  
  96.